1 using System.Collections;
2 using
System.Collections.Generic;
3 using
UnityEngine;
4
5 public
class EnemySpawner : MonoBehaviour {
6
7     
public GameObject[] enemies;
8     
public GameObject[] boss;
9     
public int count, maxCount;
10     
public bool active, isBossReady;
11     
public float time;
12
13     
private float delay;
14     
private float maxTop;
15
16
17     
void Awake(){
18         
19     }
20
21     
// Use this for initialization
22     
void Start () {
23         InitializeVariables ();
24         LimitBounds ();
25     }
26     
27     
// Update is called once per frame
28     
void Update () {
29         
if(GameplayController.instance.gameInProgress){
30             LimitBounds ();
31             SpawnEnemies ();
32             CheckedEnemies ();
33         }
34     }
35
36
37
38     
void SpawnEnemies(){
39         
if (active) {
40             
if (count != 0) {
41                 delay =
3;
42
43                 Vector3[] position =
new[] {
44                     
new Vector3 (-2, maxTop, 0),
45                     
new Vector3 (-1, maxTop, 0),
46                     
new Vector3 (0, maxTop, 0),
47                     
new Vector3 (1, maxTop, 0),
48                     
new Vector3 (2, maxTop, 0)
49                 };
50
51                 time += Time.deltaTime;
52                 
if (time > delay) {
53                     delay = Random.Range (
3, 8);
54                     
int randomEnemy = Random.Range (0, position.Length);
55                     time =
0;
56                     
for (int i = 0; i < position.Length; i++) {
57                         
if (i != randomEnemy) {
58                             Instantiate (enemies [
0], position [i], Quaternion.Euler (0, 0, 180));
59                         }
else {
60                             Instantiate (enemies [
1], position [i], Quaternion.Euler (0, 0, 180));
61                         }
62                     }
63
64                     count--;
65                 }
66                 
67             }
else {
68                 active =
false;
69             }
70
71         }
else {
72             count = maxCount;
73         }
74             
75     }
76
77     
void LimitBounds(){
78         Vector3 topBoundary = Camera.main.ViewportToWorldPoint (
new Vector3 (0, 1, 0));
79         maxTop = topBoundary.y;
80     }
81
82     
void CheckedEnemies(){
83         
if(!active){
84             
if (GameObject.FindGameObjectWithTag("Enemy") == null) {
85                 
if(!isBossReady){
86                     isBossReady =
true;
87                     GameplayController.instance.ShowWarning();
88                 }
89             }
90         }
91     }
92         
93
94     
public void SpawnBoss(){
95         Instantiate (boss[
0], new Vector3 (0, maxTop + 3, 0), Quaternion.Euler(0, 0, 180));
96     }
97
98     
public void InitializeVariables(){
99         count = maxCount;
100         time =
0f;
101         active =
true;
102         isBossReady =
false;
103     }
104 }


Use this for initialization

Update is called once per frame



Gõ tìm kiếm nhanh...